package cisdi.mes.sm.rm.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cisdi.mes.sm.rm.domain.SmRmMaterialRequest;
import cisdi.mes.sm.rm.service.MatRequestMgr;

@Controller
public class MatRequestCtl {

	@Autowired
	private MatRequestMgr matRequestMgr;
	
	public MatRequestMgr getMatRequestMgr() {
		return matRequestMgr;
	}

	public void setMatRequestMgr(MatRequestMgr matRequestMgr) {
		this.matRequestMgr = matRequestMgr;
	}

	@RequestMapping("rm/MatReqCtl/matReq.html")
	@ResponseBody
	public Map<String, Object> matReq(HttpServletRequest request,
			@RequestParam("reqSendTime") String reqSendTimeStr, @RequestParam("selectedRows") String selectedRows){
		// 定义服务器返回的表格数据
		Map<String, Object> serverInfo = new HashMap<String, Object>();
		try {
			// 3数据转换			
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date reqSendTime = dateFormat.parse(reqSendTimeStr);
			ObjectMapper objectMapper = new ObjectMapper();
			objectMapper.setDateFormat(dateFormat);
			SmRmMaterialRequest[] matReqs = objectMapper.readValue(
					selectedRows, SmRmMaterialRequest[].class);
			// 4业务处理
			serverInfo = matRequestMgr.matRequest(reqSendTime, matReqs);
		} catch (Exception e) {
			e.printStackTrace();
			serverInfo.put("success", false);
			serverInfo.put("message", e.getMessage());
		}
		return serverInfo;	
	}
	
	@RequestMapping("rm/MatReqCtl/undoMatReq.html")
	@ResponseBody
	public Map<String, Object> undoMatReq(HttpServletRequest request, @RequestParam("selectedRows") String selectedRows){
		// 定义服务器返回的表格数据
		Map<String, Object> serverInfo = new HashMap<String, Object>();
		try {
			// 3数据转换			
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			ObjectMapper objectMapper = new ObjectMapper();
			objectMapper.setDateFormat(dateFormat);
			SmRmMaterialRequest[] matReqs = objectMapper.readValue(
					selectedRows, SmRmMaterialRequest[].class);
			// 4业务处理
			serverInfo = matRequestMgr.undoMatRequest(matReqs);
		} catch (Exception e) {
			e.printStackTrace();
			serverInfo.put("success", false);
			serverInfo.put("message", e.getMessage());
		}
		return serverInfo;	
	}
}
